package com.project.dao.impl;

import com.project.bean.CutPagBean;
import com.project.bean.ProductBean;
import com.project.dao.IProductDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Repository
public class ProductDaoImpl extends BaseDao implements IProductDao {


    @Override
    public CutPagBean<ProductBean> findByItem(int pageNo, String name, LocalDate startDate, LocalDate endDate) {
        CutPagBean<ProductBean> cut = new CutPagBean<>();
        this.setConnection();
//        String sql = "select * from t_product where 1=1 ";
//        String dynasql = "";
//        if (name != null && name.length() != 0) {
//            dynasql += "and p_name like '%" + name + "%'";
//        }
//        if (startDate != null) {
//            dynasql += "and p_createDate >='" + startDate + "'";
//        }
//        if (endDate != null) {
//            dynasql += "and p_createDate <='" + endDate + "'";
//        }
//        try {
//            ps = con.prepareStatement(sql + dynasql+"limit ?,?");
//            ps.setObject(1, (pageNo - 1) * CutPagBean.PAGESIZE);
//            ps.setObject(2, CutPagBean.PAGESIZE);
//            rs = ps.executeQuery();
//            while (rs.next()){
//                cut.getList().add(getProduct());
//            }
//            ps=con.prepareStatement("select count(*) c from t_product where 1=1 "+dynasql);
//           rs= ps.executeQuery();
//            if (rs.next()){
//                System.out.println(rs.getInt("c"));
//                cut.setCount(rs.getInt("c"));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            this.closeConnection();
//        }


        String dynaSql = "where 1=1 ";
        List values = new ArrayList();
        if (name != null && name.length() != 0) {
            dynaSql += "and p_name like ? ";
            values.add("%" + name + "%");
        }
        if (startDate != null) {
            dynaSql += "and p_creatDate >=? ";
            values.add(startDate);
        }
        if (endDate != null) {
            dynaSql += "and p_createDate <= ? ";
            values.add(endDate);
        }
        try {
            ps = con.prepareStatement("select *from t_product " + dynaSql + " limit ?,?");
            for (int i = 0; i < values.size(); i++) {
                ps.setObject(i + 1, values.get((i)));
            }
            ps.setObject(values.size() + 1, (pageNo - 1) * CutPagBean.PAGESIZE);
            ps.setObject(values.size() + 2, CutPagBean.PAGESIZE);
            rs = ps.executeQuery();
            while (rs.next()) {
                cut.getList().add(this.getProduct());
            }
            ps = con.prepareStatement("select count(*) c from t_product " + dynaSql);
            for (int i = 0; i < values.size(); i++) {
                ps.setObject(i + 1, values.get((i)));
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                cut.setCount(rs.getInt("c"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cut;
    }

    @Override
    public ProductBean findById(Integer id) {
        ProductBean product = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select *from t_product where pk_productId=?");
            ps.setObject(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                product = this.getProduct();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeConnection();
        }
        return product;
    }

    private ProductBean getProduct() throws SQLException {
        ProductBean product = new ProductBean();
        product.setId(rs.getInt("pk_productId"));
        product.setName(rs.getString("p_name"));
        product.setCreatDate(LocalDate.parse(rs.getString("p_creatDate")));
        product.setPrice(rs.getInt("p_price"));
        return product;
    }

    @Override
    public void update(Integer id, Integer price) {
        this.setConnection();
        try {
            ps = con.prepareStatement("UPDATE t_product SET p_price=? WHERE pk_productId=?;");
            ps.setObject(1, price);
            ps.setObject(2, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeConnection();
        }
    }
}
